<?php
namespace app\backstage\logic;
use think\Db;
class Customer
{
    /**
     * 客服数据获取
     * */
    public function getInfo()
    {

        $customer = Db::table('quick_customer')->where('manage_id',0)
                    ->field('contact,working_time,qrcode')->findOrEmpty();

        if (request()->_role->alias == 'agent') {
            $customer_ag = Db::table('quick_customer')->where('manage_id',request()->_user->id)
                            ->field('contact,working_time,qrcode')->findOrEmpty();

            $customer = $customer_ag ? $customer_ag : $customer;
        }

        return $customer;
    }

    /**
     * 客服数据修改
     * */
    public function setInfo($params)
    {
        if (request()->_role->alias == 'agent') {
            $data['manage_id'] = request()->_user->id;
        }else{
            $data['manage_id'] = 0;
        }

        $data['contact']        = $params['contact'];
        $data['working_time']   = $params['working_time'];
        $data['qrcode']         = $params['qrcode'];

        $record = Db::table('quick_customer')->where('manage_id',$data['manage_id'])
                ->field('id')->findOrEmpty();
        if($record)
        {
            $res = Db::table('quick_customer')->where('id',$record['id'])->update($data);
        }else{
            $res = Db::table('quick_customer')->insert($data);
        }

        if($res >= 0)
        {
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

}